home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / thesrc10.zip / GETCH.C < prev    next >
C/C++ Source or Header  |  1992-08-11  |  9KB  |  253 lines

  1.  
  2. /*
  3.  * This software is Copyright (c) 1989, 1990, 1991 by Patrick J. Wolfe.
  4.  *
  5.  * Permission is hereby granted to copy, distribute or otherwise
  6.  * use any part of this package as long as you do not try to make
  7.  * money from it or pretend that you wrote it.  This copyright
  8.  * notice must be maintained in any copy made.
  9.  *
  10.  * Use of this software constitutes acceptance for use in an AS IS
  11.  * condition. There are NO warranties with regard to this software.
  12.  * In no event shall the author be liable for any damages whatsoever
  13.  * arising out of or in connection with the use or performance of this
  14.  * software.  Any use of this software is at the user's own risk.
  15.  *
  16.  * If you make modifications to this software that you feel
  17.  * increases it usefulness for the rest of the community, please
  18.  * email the changes, enhancements, bug fixes as well as any and
  19.  * all ideas to me. This software is going to be maintained and
  20.  * enhanced as deemed necessary by the community.
  21.  *
  22.  *              Patrick J. Wolfe
  23.  *              uunet!uiucuxc!kailand!pwolfe
  24.  *              pwolfe@kailand.kai.com
  25.  *
  26.  * Additions of Xenix,Sun,DOS,VMS and OS2 key handling
  27.  * made by Mark Hessling (mark@itc.gu.edu.au)
  28.  *
  29.  */
  30.  
  31.  
  32. #include <curses.h>
  33.  
  34. #if !defined(DOS) && !defined(OS2)
  35. #include "getch.h"
  36.  
  37. #define NORMAL 100
  38. #define ESCAPE 200
  39. #define FKEY   300
  40.  
  41. int my_getch (winptr)
  42. WINDOW *winptr;
  43. {
  44. int c;
  45. int state = NORMAL;
  46. int fkeycount = 0;
  47.  
  48. while (1) {
  49. #ifndef VMS
  50.        c = wgetch(winptr);
  51. #else
  52.        c = keypress();
  53. #endif
  54.        switch (state) {
  55.  
  56.        case FKEY:
  57.                switch (c) {
  58.  
  59.                /* numeric function keys */
  60.                case '0': case '1': case '2': case '3': case '4':
  61.                case '5': case '6': case '7': case '8': case '9':
  62.                        fkeycount = (fkeycount * 10) + (c - '0');
  63.                        break;
  64.  
  65.                case '~':
  66.                        switch (fkeycount) {
  67.  
  68.                        /* Find, Insert Here, Remove, Select, Prev Screen, Next Screen */
  69.                        case 1: return KEY_Find;
  70.                        case 2: return KEY_InsertHere;
  71.                        case 3: return KEY_Remove;
  72.                        case 4: return KEY_Select;
  73.                        case 5: return KEY_PrevScreen;
  74.                        case 6: return KEY_NextScreen;
  75.  
  76.                        /* unshifted vt220 function keys */
  77.                        case 17: case 18: case 19: case 20: case 21:
  78.                                return KEY_F6 + (fkeycount - 17);
  79.                        case 23: case 24: case 25: case 26:
  80.                                return KEY_F11 + (fkeycount - 23);
  81.                        case 28: case 29:
  82.                                return KEY_F15 + (fkeycount - 28);
  83.                        case 31: case 32: case 33: case 34:
  84.                                return KEY_F17 + (fkeycount - 31);
  85.  
  86.                        /* vt220 function keys - control */
  87.                        case 37: case 38: case 39: case 40: case 41:
  88.                                return KEY_SF6 + (fkeycount - 37);
  89.                        case 43: case 44: case 45: case 46:
  90.                                return KEY_SF11 + (fkeycount - 43);
  91.                        case 48: case 49:
  92.                                return KEY_SF15 + (fkeycount - 48);
  93.                        case 51: case 52: case 53: case 54:
  94.                                return KEY_SF17 + (fkeycount - 51);
  95.  
  96.                        /* shifted tvs922 function keys */
  97.         /*             case 37: case 38: case 39: case 40: case 41:
  98.                        case 42: case 43: case 44: case 45: case 46:
  99.                        case 47: case 48: case 49: case 50: case 51:
  100.                                return KEY_SF6 + (fkeycount - 37); */
  101.  
  102.                        default:
  103.                                state = NORMAL;
  104.                                }
  105.                        break;
  106.  
  107.                case 'A':       return KEY_UP;
  108.                case 'B':       return KEY_DOWN;
  109.                case 'C':       return KEY_RIGHT;
  110.                case 'D':       return KEY_LEFT;
  111.                case 'M':       return KEY_PadEnter;
  112.                case 'Z':       return KEY_BackTab;
  113.  
  114.                /* Xenix default key mappings */
  115.                case 'H':       return KEY_HOME;
  116.                case 'F':       return KEY_END;
  117.                case 'L':       return KEY_InsertHere;
  118.                case 'G':       return KEY_NextScrn;
  119.                case 'I':       return KEY_PrevScrn;
  120.  
  121.                case 'N':       return KEY_F2;
  122.                case 'O':       return KEY_F3;
  123.                case 'T':       return KEY_F8;
  124.                case 'U':       return KEY_F9;
  125.                case 'V':       return KEY_F10;
  126.                case 'W':       return KEY_F11;
  127.                case 'X':       return KEY_F12;
  128.  
  129.                /* VT[12]00 PF keys */
  130.                case 'P': case 'Q': case 'R': case 'S':
  131.                        return KEY_PF1 + (c - 'P');
  132.  
  133.                /* VT[12]00 keypad */
  134.                case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  135.                case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y':
  136.                        return KEY_PadComma + (c - 'l');
  137.  
  138.                case 'z':
  139.                        switch (fkeycount) {
  140.                        /* SUN F1-F10 */
  141.                        case 224: case 225: case 226: case 227: case 228:
  142.                        case 229: case 230: case 231: case 232: case 233:
  143.                                return KEY_F1 + (fkeycount - 224);
  144.                        /* SUN S-F1-S-F10 */
  145.                        case 324: case 325: case 326: case 327: case 328:
  146.                        case 329: case 330: case 331: case 332: case 333:
  147.                                return KEY_SF1 + (fkeycount - 324);
  148.                        case 192: return KEY_F11;
  149.                        case 193: return KEY_F12;
  150.                        case 195: return KEY_UNDO;
  151.                        case 292: return KEY_SF11;
  152.                        case 293: return KEY_SF12;
  153.                        case 214: return KEY_HOME;
  154.                        case 414: return KEY_CHOME;
  155.                        case 215: return KEY_UP;
  156.                        case 415: return KEY_CUP;
  157.                        case 216: return KEY_PrevScrn;
  158.                        case 416: return KEY_CPGUP;
  159.                        case 217: return KEY_LEFT;
  160.                        case 417: return KEY_CLEFT;
  161.                        case 219: return KEY_RIGHT;
  162.                        case 419: return KEY_CRIGHT;
  163.                        case 220: return KEY_END;
  164.                        case 420: return KEY_CEND;
  165.                        case 221: return KEY_DOWN;
  166.                        case 421: return KEY_CDOWN;
  167.                        case 222: return KEY_NextScrn;
  168.                        case 422: return KEY_CPGDN;
  169.                        case 2: return KEY_InsertHere;
  170.                        default:
  171.                                state = NORMAL;
  172.                                }
  173.                        break;
  174.  
  175.                default:
  176.                        state = NORMAL;
  177.                        }
  178.                break;
  179.  
  180.        case ESCAPE:
  181.                switch (c) {
  182.                case 'O':
  183.                case '[':
  184.                        state = FKEY;
  185.                        fkeycount = 0;
  186.                        break;
  187.  
  188.                default:
  189.                        state = NORMAL;
  190.                        }
  191.                break;
  192.  
  193.        default:
  194.                switch (c) {
  195.                case Escape:
  196.                        state = ESCAPE;
  197.                        break;
  198.  
  199.                case CSI:
  200.                        state = FKEY;
  201.                        fkeycount = 0;
  202.                        break;
  203.  
  204.                default:
  205.                        return (c);
  206.                        }
  207.                }
  208.        }
  209. }
  210. #endif
  211. #if defined(DOS) || defined(OS2)
  212. int my_getch (winptr)
  213. WINDOW *winptr;
  214. {
  215.  return(wgetch(winptr));
  216. }
  217. #endif
  218. #ifdef VMS
  219. #include iodef
  220. #include descrip
  221. /***********************************************************************/
  222. int keypress()
  223. /***********************************************************************/
  224. {
  225. /*--------------------------- local data ------------------------------*/
  226. struct { long length; char *address; } logical_name;
  227. struct { short status; short length; int remainder; } iosb;
  228.  
  229. static char kb[] = { "sys$input" };
  230. static int chan;
  231.  
  232. static char key = 0;
  233. int new_key;
  234. static int first = 1;
  235. int status;
  236. /*--------------------------- processing ------------------------------*/
  237.  
  238.  key = 0;
  239.  logical_name.length = strlen (kb);
  240.  logical_name.address = kb;
  241.  status = sys$assign (&logical_name, &chan, 0, 0);
  242.  if (status != 1)
  243.    return(-1);
  244.  status = SYS$QIOW(0, chan, IO$_READVBLK | IO$M_NOFILTR | IO$M_NOECHO
  245.         | IO$M_TIMED, &iosb, 0, 0, &key, 1,600, 0,0, 0, 0);
  246.  if (!key)
  247.     return (0);
  248.  new_key = (int)(unsigned)(key);
  249.  status = sys$dassgn (chan);
  250.  return (new_key);
  251. }
  252. #endif
  253.